package com.sita.tboard.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.sita.bike.R;


/**
 * Created by LiJuan on 15/10/20.
 */
public class SmallDial extends View {

    private Bitmap backBitmap, runBitmap, pointerBitmap, power_backBitmap, power_runBitmap;
    private Paint mBackPaint, mRunPaint;
    private RectF mOval;
    private float mAngle = 0;

    public SmallDial(Context context) {
        super(context);
        init();
    }


    public SmallDial(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        backBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tboard_smalldial_background, options);
        runBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tboard_smalldial_run, options);
        pointerBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tboard_smalldial_pointer, options);
        power_backBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tboard_power_background, options);
        power_runBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tboard_power_run, options);
        mBackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//消除锯齿
        mRunPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Shader shader = new BitmapShader(runBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//CLAMP 拉伸
        mRunPaint.setShader(shader);
        mOval = new RectF(0, 0, runBitmap.getWidth(), runBitmap.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        RectF dst = new RectF(0, 0, backBitmap.getWidth(), backBitmap.getHeight());
        canvas.drawBitmap(backBitmap, null, dst, mBackPaint);
        canvas.drawArc(mOval, 107, -(float) 3.2 * (mAngle / 100), true, mRunPaint);
        canvas.drawBitmap(power_backBitmap, null, dst, mBackPaint);
        canvas.drawBitmap(power_runBitmap, null, dst, mBackPaint);

        canvas.save();
//        if(mAngle<263.6&&8<mAngle){
//            canvas.rotate(-mAngle+4, pointerBitmap.getWidth() / 2, pointerBitmap.getHeight() / 2);
//        }else if(mAngle>=263.6){
//            canvas.rotate(-((float)(263.6-3)), pointerBitmap.getWidth() / 2, pointerBitmap.getHeight() / 2);
//        }else if(mAngle<=8){
//            canvas.rotate(0, pointerBitmap.getWidth() / 2, pointerBitmap.getHeight() / 2);
//        }

        if (mAngle == 0) {
            canvas.rotate(0, pointerBitmap.getWidth() / 2, pointerBitmap.getHeight() / 2);
        } else {

            canvas.rotate(-(float) 3.2 * (mAngle / 100) - 4, pointerBitmap.getWidth() / 2, pointerBitmap.getHeight() / 2);
        }

//        canvas.rotate(-mAngle+4, pointerBitmap.getWidth() / 2, pointerBitmap.getHeight() / 2);
        Log.d("角度", mAngle + "");
        canvas.drawBitmap(pointerBitmap, 0, 0, mRunPaint);
        canvas.restore();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredHeight = runBitmap.getHeight();

        int measuredWidth = runBitmap.getWidth();

        setMeasuredDimension(measuredHeight, measuredWidth);
    }

    public void setAngle(float angle) {
        this.mAngle = angle;
        invalidate();
    }
}